// pages/wode/collect/collect.js
//index.js
//获取应用实例
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoContext: null,
    hidevido: true,
    videoSrc: '',
    url: 0,
    tab: [
      {
        name: '百科',
        url: '/pages/wode/collect/collect',
        active: false,
        fun: 'redirectTo'
      },
      // {
      //   name: '专家',
      //   url: '/pages/wode/collect/zhuanjia',
      //   active: false,
      //   fun: 'redirectTo'
      // },
      {
        name: '视频',
        url: '/pages/wode/collect/shipin',
        active: true,
        fun: 'redirectTo'
      },
      {
        name: '音频',
        url: '/pages/wode/collect/yinpin',
        active: false,
        fun: 'redirectTo'
      },
      {
        name: '论坛',
        url: '/pages/wode/collect/luntan',
        active: false,
        fun: 'redirectTo'
      }
    ]
  },
  showActivety: function (e) {
    var $this = this;
    var urls = njzxw.getDataVal(e, 'url');
    wx.showActionSheet({
      itemList: ['播放', '取消收藏'],
      success: function (res) {
        if (res.tapIndex == 0) {
          $this.showVideo(e);
        } else if (res.tapIndex == 1) {
          $this.deleteCommon(e);
        }
      }
    })
  },
  /**
   * 取消收藏
   */
  deleteCommon: function (e) {
    var res_id = njzxw.getDataVal(e, 'id');
    var type = njzxw.getDataVal(e, 'stype');
    var $this = this;
    njzxw.alert({
      msg: '确定需要取消收藏吗？',
      confirm: true,

      fun: function (a) {

        if (a) {
          njzxw.ajax({
            url: url.encyclopediaInfoFront.deleteShouChang,
            data: {
              id: res_id,
              type: type
            },
            load: true,
            fun: function (a, b) {
              wx.hideLoading();
              if (a) {
                if (b.data.state == 1) {
                  njzxw.alert({
                    msg: '操作成功',
                    fun: function () {
                      njzxw.loadStart($this);
                      $this.loadData($this);
                    }
                  });

                } else {
                  njzxw.alert({
                    error: true,
                    msg: '删除失败'
                  });
                }
              } else {
                njzxw.alert({
                  error: true,
                  msg: '操作失败'
                });
              }
            }
          });
        }
      }
    });
  },
  /**
     * 空值视频播放器的显示
     */
  showVideo: function (e) {
    console.log(e);
    try {
      this.data.videoContext.stop();
    } catch (e) { }
    var num = njzxw.getDataVal(e, 'num');
    console.log(num, this.data.list[num]);
    var src = this.data.list[num].res_path;
    if (src == undefined || src == '') {
      src = this.data.list[num].url;
    }
    this.setData({
      hidevido: false,
      videoSrc: src
    });
    this.data.videoContext.play();

    // e.currentTarget.dataset
    this.dianzhan(e);
  },
  /**
   * 空值视频播放器的显示
   */
  hideVideo: function () {
    this.setData({
      hidevido: true
    });
    this.data.videoContext.pause();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.videoContext = wx.createVideoContext('vido');
    njzxw.initFun(this);
    this.loadDatas(this);
  },
  loadDatas: function ($this) {
    njzxw.loadStart(this);
    this.loadData(this);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  loadData: function ($this) {
    njzxw.loadData($this, url.resourcesInfoFront.queryshoucangPage, {
      pageSize: 20,
      res_type: 1,
      stype:4
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadDatas(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData(this);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})